package controller;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BookDetailsDTO;
import model.UserDTO;
import DAO.BookDetailsDAO;
import DAO.DAOFactory;
import DAO.NotFoundException;

/**
 * Servlet implementation class BookingHistoryController
 */
@WebServlet("/BookingHistoryController")
public class BookingHistoryController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookingHistoryController() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			doProcess(request, response);
		} catch (NotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			doProcess(request, response);
		} catch (NotFoundException e) {
			e.printStackTrace();
		}

	}


	private void doProcess(HttpServletRequest req, HttpServletResponse res) throws NotFoundException {
		UserDTO user = (UserDTO)req.getSession().getAttribute("LoggedinUser");
		int userId = user.getUserId();
		
		ArrayList<BookDetailsDTO> table2;
		BookDetailsDAO h = DAOFactory.loadInstance().getBookDetailsDAO();
		table2 = h.findBookingHistoryById(userId);
		req.setAttribute("booking", table2);
		RequestDispatcher rd = req.getRequestDispatcher("/BookingHistory.jsp");
		try {
			rd.forward(req, res);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
